package softtech.apps.callblocker.adapter;

import softtech.apps.utility.ApplicationConstants;
import softtech.apps.utility.FileUtility;
import softtech.apps.callblocker.ActivityDashboard;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.TextEnable;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This adapter control the flow of this application
 * 
 * @author tdinhphuoc
 * 
 */
public class AdapterHomeManager extends BaseAdapter {

	private Context mContext;

	private int mode;

	public AdapterHomeManager(Context context) {
		mContext = context;
		mode = ApplicationConstants.APP_NOT_ACTIVATED;
	}

	@Override
	public int getCount() {
		if (mode == ApplicationConstants.APP_ACTIVATED) {
			return 5;
		} else {
			return 6;
		}

	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_home_manager, parent,
					false);

			viewHolder = new ViewHolder();
			viewHolder.tvNameManager = (TextView) convertView
					.findViewById(R.id.tvNameManager);
			viewHolder.tvDecriptionManager = (TextView) convertView
					.findViewById(R.id.tvDecriptionManager);
			viewHolder.imgLogoManager = (ImageView) convertView
					.findViewById(R.id.imgLogoManager);

			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvNameManager, viewHolder.tvNameManager);
			convertView.setTag(R.id.tvDecriptionManager,
					viewHolder.tvDecriptionManager);
			convertView.setTag(R.id.imgLogoManager, viewHolder.imgLogoManager);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		insertContentView(viewHolder, position);

		return convertView;
	}

	static class ViewHolder {
		protected TextView tvNameManager, tvDecriptionManager;
		protected ImageView imgLogoManager;

	}

	/**
	 * content of adapter
	 * 
	 * @param viewHolder
	 * @param position
	 */
	void insertContentView(ViewHolder viewHolder, int position) {
		if (mContext == null) {
			return;
		}
		if (mode == ApplicationConstants.APP_ACTIVATED) {
			switch (position) {
			case 0:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_home_enable));
				viewHolder.imgLogoManager.setImageResource(R.drawable.ic_x1);
				break;

			case 1:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_blacklist));
				viewHolder.imgLogoManager.setImageResource(R.drawable.ic_x1);
				break;
			case 2:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_history));
				viewHolder.imgLogoManager.setImageResource(R.drawable.ic_x1);
				break;
			case 3:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_setting));
				viewHolder.imgLogoManager.setImageResource(R.drawable.ic_x1);
				break;
			case 4:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_about_us));
				viewHolder.imgLogoManager.setImageResource(R.drawable.ic_x1);
				break;

			default:
				break;
			}
		} else {
			switch (position) {
			case 0:
				Intent inWidget = new Intent("callblocker.widget.ACTIVE");
				mContext.sendBroadcast(inWidget);
				viewHolder.tvDecriptionManager.setVisibility(View.VISIBLE);
				if (TextEnable.getEnableApp(mContext) == 0) {

					viewHolder.tvNameManager.setText(mContext.getResources()
							.getString(R.string.dashboard_home_enable));
					viewHolder.imgLogoManager
							.setImageResource(R.drawable.ic_enable);
					viewHolder.tvDecriptionManager.setText(mContext
							.getResources().getString(R.string.app_is_disable));
				} else {
					viewHolder.tvNameManager.setText(mContext.getResources()
							.getString(R.string.dashboard_home_disable));
					viewHolder.imgLogoManager
							.setImageResource(R.drawable.ic_enable);
					viewHolder.tvDecriptionManager.setText(mContext
							.getResources().getString(R.string.app_is_enable));
				}

				break;
			case 1:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_home_buy_app));
				viewHolder.tvDecriptionManager.setVisibility(View.VISIBLE);
				viewHolder.tvDecriptionManager.setText(mContext.getResources()
						.getString(R.string.dashboard_home_buy_app));
				viewHolder.imgLogoManager
						.setImageResource(R.drawable.ic_buy_app);
				break;
			case 2:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_blacklist));
				viewHolder.imgLogoManager
						.setImageResource(R.drawable.ic_blacklist);
				break;
			case 3:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_history));
				viewHolder.imgLogoManager
						.setImageResource(R.drawable.ic_history);
				break;
			case 4:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_setting));
				viewHolder.imgLogoManager
						.setImageResource(R.drawable.ic_setting);
				break;
			case 5:
				viewHolder.tvNameManager.setText(mContext.getResources()
						.getString(R.string.dashboard_about_us));
				viewHolder.imgLogoManager
						.setImageResource(R.drawable.ic_about_us);
				break;

			default:
				break;
			}
		}

	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public int getFreeDate(Context context) {
		FileUtility futil = new FileUtility();

		//
		// init file limited
		//
		int limited_date = 30;
		try {

			limited_date = Integer.parseInt(futil.LoadContentFile(context,
					ApplicationConstants.FILE_LIMITED));

			if (limited_date == 0) {
				futil.SaveContentFile(context,
						ApplicationConstants.FILE_LIMITED, "" + limited_date);
			}
		} catch (Exception e) {
			futil.SaveContentFile(context, ApplicationConstants.FILE_LIMITED,
					"" + limited_date);
		}
		return limited_date;
	}

}
